﻿using System.Reflection;
using MomentumPlugin.Common;
using Plugins.UserControls;
using StockModel;
using System;

namespace MomentumPlugin.Historical
{
	internal class ControlPanelVM : Plugins.Base.Historical.ControlPanelVMBase
	{
		#region Attributes

		private readonly HistoricalSettings _settings;

		#endregion

		#region Properties

		public PresetSelectorVM<HistoricalSettings> PresetSelectorVM { get; private set; }
		public StrategyControlVM StrategyControlVM { get; private set; }

		#endregion

		#region Public Methods

		public ControlPanelVM(RawStockDataSource rawStockDataSource, HistoricalEngine engine) : base(rawStockDataSource, engine)
		{
			this.StrategyControlVM = new StrategyControlVM();

			// Create settings
			_settings = new HistoricalSettings(this.HourSelectorVM.Data, this.StockSelectorVM.Data,this.StockParametersSelectorVM.Data, this.DateSelectorVM.Data, this.StrategyControlVM.Data);
			engine.Settings = _settings;

			this.SetDefaultValues();

			this.PresetSelectorVM = new PresetSelectorVM<HistoricalSettings>(Assembly.GetExecutingAssembly().GetName().Name + Properties.Settings.Default.HistoricalPresetsFilename, engine.Settings);
		}

		protected override bool CanRunStrategy()
		{
			return base.CanRunStrategy() && !this.StrategyControlVM.HasValidationErrors;
		}

		#endregion

		#region Private Methods

		private void SetDefaultValues()
		{
			_settings.StockParameters.PercentageFee = 0m;
			_settings.StockParameters.FixedFeePerItem = 0.9m;
			_settings.StockParameters.FixedReserve = 320;
			_settings.DateSettings.UseAllDays = true;
			_settings.HourOptions.StartWithOpening = true;
			_settings.HourOptions.LastOpen = new TimeSpan(17, 0, 0);
			_settings.HourOptions.ForcedCloseTime = new TimeSpan(17, 29, 0);
			_settings.StockParameters.ValueMultiplier = 1;
			_settings.StrategyControlData.Volume = 1;
		}

		#endregion
	}
}
